package com.google.code.sprite.generate;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.google.code.sprite.conf.DbConfig;

public class TableMetaData {

    private Map<String, ColumnMetaData> fields = new HashMap<String, ColumnMetaData>();
    private String name;

    public TableMetaData(String tableName) {
        this.name = tableName;
        Connection conn = DbConfig.getConnect();
        ResultSet rs = null;
        try {
            rs = conn.getMetaData().getColumns(null, null, tableName, "%");
            while (rs.next()) {
                ColumnMetaData col = new ColumnMetaData();
                String name = rs.getString("COLUMN_NAME");
                col.setColumnName(name);
                col.setDataType(rs.getInt("DATA_TYPE"));
                fields.put(name,col);
            }
        } catch (SQLException e) {
        } finally {
            if (rs != null) try { rs.close(); } catch (Exception e) {}
            if (conn !=null ) try { conn.close(); } catch (Exception e) {}
        }
    }

    public Map<String, ColumnMetaData> getFieldsInfo() {
        return this.fields;
    }

    public String getTableName() {
        return this.name;
    }

}

